package com.oedtech.esl.config;

import com.oedtech.esl.filter.ValidateCodeFilter;
import com.oedtech.esl.handler.LoginFailHandler;
import com.oedtech.esl.handler.LoginSuccessHandler;
import com.oedtech.esl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()   //取消post csrf验证

                .authorizeRequests()
                .antMatchers("/dist/**","/bootstrap/**","/plugins/**","/build/**","/fonts/**","/js/**","*.woff*","*.ttf*").permitAll()
//                .antMatchers("/dist/**","/bootstrap/**","/plugins/**","/build/**","/fonts/**","/js/**").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/pin/**").permitAll()
                .antMatchers("/api3/**").permitAll()
                .antMatchers("/user_rest/**","/menu/user_manager").access("hasAnyRole('ADMIN')")
                .antMatchers("/role_rest/**","/menu/role_manager").hasAnyRole("SUPPERADMIN")

                .anyRequest().authenticated()
                .and()
                .addFilterBefore(new ValidateCodeFilter(), UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login/form")     //登录请求表单提交路径
                .defaultSuccessUrl("/esl_manager")
                .failureUrl("/login?error")
//                .successForwardUrl("/esl_manager")  //成功登录后转到指定页面
                .failureHandler(new LoginFailHandler())
                .successHandler(new LoginSuccessHandler())
                .permitAll()
                .and()

                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .permitAll()
                .and().headers().cacheControl().disable();  //禁用网页缓存控制

//                http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
    }

    @Autowired
    private UserService userService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth
//                .inMemoryAuthentication()
//                .withUser("admin").password("123456").roles("USER");

        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
    }
}
